package com.slj.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slj.comm.responce.PageResult;
import com.slj.comm.util.FileUploadUtil;
import com.slj.model.query.ProductQuery;
import com.slj.model.entity.Products;
import com.slj.mapper.ProductsMapper;
import com.slj.model.vo.ProductVo;
import com.slj.service.ProductsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 供应商商品信息表 服务实现类
 * </p>
 *
 * @author 礼俊
 * @since 2025-09-20 22:46
 */
@Service
@Slf4j
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService {
    @Autowired
    private ProductsMapper productsMapper;

    @Autowired
    private FileUploadUtil fileUploadUtil;
    /**
     * 分页查询供应商商品信息
     *
     * @param productQuery
     * @return
     */
    @Override
    public PageResult pageList(ProductQuery productQuery) {
        Page<ProductVo> page = new Page<>(productQuery.getPageNum(), productQuery.getPageSize());
       Page<ProductVo> pageVO= productsMapper.pageList(page, productQuery);
        return PageResult.of(pageVO) ;
    }

    @Override
    public void editProduct(Products products, MultipartFile imageUrl) {
        log.info("编辑商品：{}", products);
        log.info("id:{}", products.getProductId());
        //图片处理
        String imageUrlPath = null;
        if (imageUrl != null) {
            try {
                imageUrlPath = fileUploadUtil.uploadImage(imageUrl);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        products.setImageUrl(imageUrlPath);

        this.saveOrUpdate(products);
    }

    @Override
    public PageResult<ProductVo> getProductPage(ProductQuery productQuery) {
        Page<ProductVo> page = new Page<>(productQuery.getPageNum(), productQuery.getPageSize());
        Page<ProductVo> productPage = productsMapper.getProductPage(page, productQuery);
        return PageResult.of(productPage);
    }
}
